package com.apes.fico.finance.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.fico.engine.repository.BusinessDocumentRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.outbox.OutboxPayloadWrapper;
import com.apes.framework.plugin.outbox.OutboxSubscriberService;
import com.apes.framework.plugin.outbox.annotation.OutboxSubscriber;
import com.apes.framework.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Title: GLVoucherPushService
 * @Description: 异常凭证推送消费处理
 * @author: ZhengZeXian
 * @create: 2022/3/14 14:39
 */
@Service("gLVoucherPushService")
public class GLVoucherPushService extends DomainService {

    @Autowired
    private BusinessDocumentRepository documentRepository;

    /**
     * 推送事务发件箱
     * @param request
     */
    public void push(SimpleRequest request){
        JSONObject jsonObject = request.getJO();
        String businessType = (String) jsonObject.getJSONObject("businessType").get("innerBusinessKey");
        JSONArray items = jsonObject.getJSONArray("items");
        for (int i = 0; i < items.size(); i++) {
            JSONObject item = items.getJSONObject(i);
            String id = item.getString("id");
//            documentRepository.deleteByBusinessId(id);
            //推送凭证数据消费信息
            SpringManager.getBean(OutboxSubscriberService.class)
                    .commonSendRequest("local:GLVoucher.pushVoucherMessage",
                            "pushVoucherMessage" + id , new JSONObject(MapUtil.mapper(
                                    "id", id,"businessType",businessType
                            )));
        }
    }

    @OutboxSubscriber(id="local:GLVoucher.pushVoucherMessage", name="推送凭证数据消费信息")
    public void pushVoucherMessage(OutboxPayloadWrapper outboxPayloadWrapper){
        JSONObject object = JSONObject.parseObject(outboxPayloadWrapper.getPayload());
        String id = object.getString("id");
        String businessType = object.getString("businessType");

        Map map = MapUtil.mapper(
                "businessType", businessType,
                "subscriberId", "fin.engine." + businessType,
                "id",id
        );
        this.invoke("outbox.message.resend",map);
    }



    public Map importData(SimpleRequest request) throws Exception {

        JSONObject jsonData =  request.getJO();
        JSONObject importData = jsonData.getJSONObject("importData");
        JSONObject model = jsonData.getJSONObject("model");

        Map requestMap = new LinkedHashMap();
        List<Map> items = new ArrayList<>();
        StringBuffer msg = new StringBuffer();
        importData.forEach((k, v) -> {

            JSONArray excl = (JSONArray) v;
            JSONArray header = (JSONArray) excl.get(0); //表格头部

            for (int i = 0; i < excl.size(); i++) {  //遍历每一行
                if (i == 0) continue; //跳过首行

                JSONArray lineData = (JSONArray) excl.get(i); //当前行
                if (lineData.size() == 0) continue;

                Map m = MapUtil.mapper(
                        "id", header.indexOf("单据号码") < lineData.size() ? lineData.getString(header.indexOf("单据号码")) : null
                );

                //无处理异常消息
                msg.append("");
                items.add(m);

            }
        });
        requestMap.put("msg",msg.toString());
        requestMap.put("items",items);
        return requestMap;
    }
}
